package com.wasion.gydpe.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import com.wasion.gydpe.model.Resource;
import com.wasion.gydpe.service.WelcomeService;

/**
 * 
 * @ClassName: WelcomeAction
 * @Description: 用户登录和菜单加载等初始化控制层Action
 * @author 高意
 * @date Sep 18, 2012 10:27:04 AM
 * 
 */
@ParentPackage("gydpe-default")
@Namespace("/welcome")
@Results( { @Result(name = "success", location = "/jsp/welcome/index.jsp"),
		@Result(name = "error", location = "/jsp/common/error/error.jsp") })
@ExceptionMappings( { @ExceptionMapping(exception = "java.lang.Exception", result = "error") })
@Controller
public class WelcomeAction extends BaseAction {

	// 序列化版本Id
	private static final long serialVersionUID = 8334656901194694796L;

	// 日志，slf4j+lokback
	private static final Logger LOGGER = LoggerFactory
			.getLogger(WelcomeAction.class);

	// 服务接口，由于用到了json-default，不要定义get方法，
	@javax.annotation.Resource
	private WelcomeService welcomeService;

	// 返回前端的json对应的属性需要定义set和get方法，ognl
	private List<Resource> menu; // 菜单
	private String rsPId; // 父资源id
	private String j_username; // 用户名
	private String j_password; // 用户密码

	public void setRsPId(String rsPId) {
		this.rsPId = rsPId;
	}

	public List<Resource> getMenu() {
		return menu;
	}

	public void setMenu(List<Resource> menu) {
		this.menu = menu;
	}

	@Action(value = "login")
	public String login() {

		/*Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("username", j_username);
		paramsMap.put("pwd", j_password);

		try {
			boolean isSucc = welcomeService.login(paramsMap);
			if (!isSucc) {
				return "error";
			}

			return "success";
		} catch (Exception e) {

			e.printStackTrace();
			LOGGER.error("登录失败：login >>>>>> " + e);
			return "error";
		}*/
		
		return "success";
	}

	/**
	 * 
	 * @Title: initMainMenu
	 * @Description: 加载主菜单
	 * @param
	 * @return
	 * @throws
	 */
	public String initMainMenu() {

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("username", "admin");
		paramMap.put("isMainMenu", "1");
		try {
			menu = welcomeService.queryResources(paramMap);
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("查询功能菜单资源失败：initMainMenu >>>>>> " + e);
		}

		return SUCCESS;
	}

	/**
	 * 
	 * @Title: initSubMenu
	 * @Description: 加载子菜单
	 * @param
	 * @return
	 * @throws
	 */
	public String initSubMenu() throws Exception {

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("username", "admin");
		paramMap.put("isMainMenu", "0");
		paramMap.put("rsPId", rsPId);
		menu = welcomeService.queryResources(paramMap);

		return SUCCESS;
	}

	public String getJ_username() {
		return j_username;
	}

	public void setJ_username(String j_username) {
		this.j_username = j_username;
	}

	public String getJ_password() {
		return j_password;
	}

	public void setJ_password(String j_password) {
		this.j_password = j_password;
	}
}
